package net.zjitc.service.UserServiceImpl;

import net.zjitc.dao.UserDao;
import net.zjitc.dao.UserDaoImpl.UserDaoImpl;
import net.zjitc.daomain.User;
import net.zjitc.service.UserService;

import java.sql.SQLException;

/**
 * Created by hasee on 2017/9/27.
 */
public class UserServiceImpl implements UserService{
    @Override
    public User login(String username,String password) throws SQLException {
        UserDao dao = new UserDaoImpl();
        User user = dao.findByUsername(username);
        if(user!=null&&user.getPassword().equals(password)){//如果存在该用户,且密码正确
            return user;
        }
        return null;
    }
    @Override
    public User logon(String username,String password) throws SQLException {
        UserDao dao = new UserDaoImpl();
        User user = dao.findByUsername(username);
        if(user==null&&username!=""&&password!=""){//如果不存在该账号,且输入的账号,密码不为空
            User newuser = new User();//新建一个用户
            newuser.setUsername(username);//设置账号，密码
            newuser.setPassword(password);
            dao.insert(newuser);//把用户信息插进数据库
            return newuser;
        }
        return null;
    }
}
